package amushen.tree;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import vo.ProductCate;

import amushen.base.BaseBO;

public abstract class TreeAdapter extends BaseBO implements ITree {
	
	//define vo name in order to do insert, delete, update, move operations
	protected String voName="";	
	
	public int deleteNode(String id) {
		if(voName==null||voName.length()<1)return 0;
		
		return 1;
	}

	public Object getAttribute(String id, String attributeName) {
		// TODO Auto-generated method stub
		return null;
	}

	public Map getAttributes(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	public abstract TreeNode[] getRoot();

	public String getRootHtml(){
		TreeNode[] nodes=getRoot();
		String tree="{label:'name',identifier: 'objectId',items: [\n";		
		for(int i=0;i<nodes.length;i++){			
			tree+="{ objectId :'"+nodes[i].getId()+"',name:'"+nodes[i].getName()+"',type:'root'";
			tree+=",leaf:'"+(nodes[i].getIsLeaf())+"'";
			tree+=mapToString(nodes[i].getAttributes());
			TreeNode[] children = getSub(nodes[i].getId());			
			if(children!=null&&children.length>1){
				tree+=",children:";
				if(children.length>2)
					tree+="[";
				for(int j=1;j<children.length;j++){					
					tree+="{_reference:'"+children[j].getId()+"'},";
				}					
				tree=tree.substring(0,tree.length()-1);	//remove last comma
				if(children.length>2)
					tree+="]";
				tree+="},\n";
				for(int j=1;j<children.length;j++){					
					tree+="{objectId:'"+children[j].getId()+"',name:'"+children[j].getName()+"',type:'stub'},";
				}				
			}
			else{
				tree+=",leaf:'1'},";
			}			
		}
		tree=tree.substring(0,tree.length()-1);	//remove last comma		
		return tree+"]}";
	}

	/**
	 * the first treeNode must be self
	 */
	public abstract TreeNode[] getSub(String id);

	public String getSubHtml(String id){
		TreeNode[] nodes=getSub(id);
		if(nodes==null||nodes.length<1)return "";
		StringBuffer tree=new StringBuffer();
		tree.append("{");		
		try{
			tree.append("objectId:'")
				.append(id).append("',name:'").append(nodes[0].getName())				
				.append("',leaf:'").append(nodes[0].getIsLeaf()).append("'");
			tree.append(mapToString(nodes[0].getAttributes()));
			if(nodes.length>1){
				tree.append(",children:");
				if(nodes.length>2)
					tree.append("[");
				for(int j=1;j<nodes.length;j++){					
					tree.append("{stub:'"+nodes[j].getId()+"'},");
				}
				tree.deleteCharAt(tree.length()-1);
				if(nodes.length>2)
					tree.append("]");
				tree.append("}\n");				
			}else{
				tree.append("}\n");
			}
		}catch(Exception e){
			return "";
		}		
		return tree.toString();
	}

	public int insertChild(String id, Object child) {
		// TODO Auto-generated method stub
		return 0;
	}

	public int moveTo(String id, String newParentId) {
		// TODO Auto-generated method stub
		return 0;
	}

	public int updateNode(String id, Object node) {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * convert a map to a string
	 * this is used to generate attribute string
	 * @param map
	 * @return
	 */
	protected String mapToString(Map map){
		String rst="";		
		if(map==null)return rst;
		for(Iterator i=map.keySet().iterator();i.hasNext();){
			String key=(String)i.next();
			String value=(String)map.get(key);
			rst+=","+key+":'"+value+"'";
		}
		return rst;
	}
}
